package com.xiaoxx.rabbitmq.db.service;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxx.rabbitmq.constant.BrokerMessageStatus;
import com.xiaoxx.rabbitmq.db.entity.BrokerMessage;
import com.xiaoxx.rabbitmq.db.mapper.BrokerMessageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class MessageStoreService extends ServiceImpl<BrokerMessageMapper, BrokerMessage>{

	@Resource
	private BrokerMessageMapper brokerMessageMapper;

	
	public void updateStatus(String messageId, BrokerMessageStatus messageStatus) {
		new LambdaUpdateChainWrapper<>(this.brokerMessageMapper).eq(BrokerMessage::getMessageId, messageId)
				.set(BrokerMessage::getStatus, messageStatus.getCode())
				.set(BrokerMessage::getUpdateTime, LocalDateTime.now()).update();
	}

	public void successByIsStatus(String messageId, BrokerMessageStatus messageStatus) {
		new LambdaUpdateChainWrapper<>(this.brokerMessageMapper).eq(BrokerMessage::getMessageId, messageId)
				.eq(BrokerMessage::getStatus, BrokerMessageStatus.SENDING.getCode())
				.set(BrokerMessage::getStatus, messageStatus.getCode())
				.set(BrokerMessage::getUpdateTime, LocalDateTime.now()).update();
	}

	public void copCountIncrease(String messageId) {
		this.lambdaUpdate()
				.eq(BrokerMessage::getMessageId, messageId)
				.setSql("cop_count = cop_count + 1")
				.set(BrokerMessage::getUpdateTime, LocalDateTime.now())
				.update();
	}
}
